﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace THenu.TinyHourseLib
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            msgToolTip.ToolTipTitle = "嘟肚侠";
            msgToolTip.ToolTipIcon = ToolTipIcon.Error;

            msgToolTip.SetToolTip(richTextBox1, "嘟肚侠");

            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);

            this.HandleDestroyed += new EventHandler(Form1_HandleDestroyed);

        }

        void Form1_HandleDestroyed(object sender, EventArgs e)
        {
   
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            backgroundWorker1.RunWorkerAsync();

            timerActionDirector.Start();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

                //ManagementObjectSearcher monitoer = new ManagementObjectSearcher("Select * from Win32_Process");

                //ManagementObjectCollection objCollection = monitoer.Get();
                //foreach (ManagementObject item in objCollection)
                //{

                //    string msg = string.Format("{0}\t{1}", item["Name"].ToString(), item["PID"].ToString()) + Environment.NewLine;
                //    if (richTextBox1.InvokeRequired)
                //    {

                //        richTextBox1.BeginInvoke(new Action(() => { richTextBox1.AppendText(msg); }));
                //    }
                //    else
                //    {
                //        richTextBox1.AppendText(msg); 
                //    }
                //}
                while (true)
                {
                    Thread.Sleep(55);
                    if (this.InvokeRequired)
                    {
                        this.Invoke(new Action(() => { msgToolTip.Show("嘟嘟", this, Cursor.Position, 1000); }));
                    }
                }

        }
        private bool threadExit = false;
        private ToolTip msgToolTip = new ToolTip();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerActionDirector_Tick(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                backgroundWorker1.CancelAsync();
            }
        }
        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("000214F9-0000-0000-C000-000000000046")]
        public interface IShellLinkW
        {
            [PreserveSig]
            int GetPath(StringBuilder pszFile, int cch, [In, Out]ref WIN32_FIND_DATAW pfd, uint fFlags);
            [PreserveSig]
            int SetPath([MarshalAs(UnmanagedType.LPWStr)]string pszFile);
            [PreserveSig]
            int GetIDList([Out]out IntPtr ppidl);
            [PreserveSig]
            int SetIDList([In]ref IntPtr pidl);
            [PreserveSig]
            int GetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
            [PreserveSig]
            int GetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
            [PreserveSig]
            int SetArguments(StringBuilder pszArgs, int cch);
            [PreserveSig]
            int GetHotKey(ushort wHotKey);
            [PreserveSig]
            int GetShowCmd([Out]out int piShowCmd);
            [PreserveSig]
            int SetShowCmd(int iShowCmd);
            [PreserveSig]
            int GetIconLocation(StringBuilder pszIconPath, int cch, [Out]out int piIcon);
            [PreserveSig]
            int SetIconLocation([MarshalAs(UnmanagedType.LPWStr)]string pszIconPath, int iIcon);
            [PreserveSig]
            int SetRelativePath([MarshalAs(UnmanagedType.LPWStr)]string pszPathRel, uint dwReserved);
            [PreserveSig]
            int Resolve(IntPtr hwnd, uint fFlags);
        }
        [DllImport("shell32.dll", ExactSpelling = true)]
        public static extern int SHOpenFloderAndSelectItems(IntPtr pidlFloder, uint cidl, [In, MarshalAs(UnmanagedType.LPArray)]IntPtr apidl, uint dwFlags);

        [DllImport("ole32.dll", ExactSpelling = true)]
        public static extern int CoCreateInstance([In]ref Guid rclsid, IntPtr pUnkOuter, CLSCTX dwClsContent, [In]ref Guid riid, [Out]out IntPtr ppv);
        private void pictureBox1_Click(object sender, EventArgs e)
        {

            try
            {

                ProcessStartInfo psInfo = new ProcessStartInfo("explorer.exe", "/SELECT,C:\\ImbaMallLog.txt");
                
                Process.Start(psInfo);

                //Guid CLSID_ShellLink = new Guid("00021401-0000-0000-C000-000000000046");
                //Guid II_IShellLink = new Guid("000214F9-0000-0000-C000-000000000046");

                //IntPtr ppsl = IntPtr.Zero;
                //int result = CoCreateInstance(ref CLSID_ShellLink, IntPtr.Zero, CLSCTX.INPROC_SERVER, ref II_IShellLink, out ppsl);

                //IShellLinkW psl = Marshal.GetObjectForIUnknown(ppsl) as IShellLinkW;

                //psl.SetPath("C:\\ImbaMallLog.txt");
                //IntPtr pidl = IntPtr.Zero;
                //psl.GetIDList(out pidl);
                //int state = SHOpenFloderAndSelectItems(pidl, 0,IntPtr.Zero, 0);

                //Marshal.FreeCoTaskMem(ppsl);
                //Marshal.Release(ppsl);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }




        }

    }
    public enum CLSCTX : uint
    {
        INPROC_SERVER = 0x1
    }
}
